package zhai.docking.alipay.service.base.alipay.api;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayMerchantIotDeviceBindRequest;
import com.alipay.api.request.AlipayMerchantIotDeviceQueryRequest;
import com.alipay.api.response.AlipayMerchantIotDeviceBindResponse;
import com.alipay.api.response.AlipayMerchantIotDeviceQueryResponse;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhai.docking.alipay.constant.AlipayConstant;
import zhai.docking.alipay.constant.LogConstant;
import zhai.docking.alipay.domain.base.alipay.BaseAlipayIotBindIn;
import zhai.docking.alipay.domain.base.alipay.BaseAlipayIotBindOut;
import zhai.docking.alipay.domain.base.alipay.BaseAlipayIotQueryIn;
import zhai.docking.alipay.domain.base.alipay.BaseAlipayIotQueryOut;
import zhai.docking.alipay.domain.base.alipay.common.BaseAlipayCommonOut;
import zhai.docking.alipay.domain.base.bizlog.BizLogDataTemp;
import zhai.docking.alipay.domain.base.bizlog.BizLogSave;
import zhai.docking.alipay.prop.AlipayApiProperties;
import zhai.docking.alipay.service.base.bizlog.BizLogSer;
import zhai.docking.alipay.util.JSONUtil;
import zhai.docking.alipay.util.ObjectUtil;
import zhai.docking.alipay.util.StringUtil;

/**
 * @author Created by 翟凤玺
 * @package zhai.docking.alipay.service.base.alipay.api
 * @project-name zhai-docking-alipay
 * @description 基础-支付宝-IOT 服务实现
 * @create-datetime 2020/12/31 15:43
 */
@Slf4j
@Service
public class BaseAlipayApiIotSerImpl implements BaseAlipayApiIotSer {

    private static final String BUSINESS_NAME = "【基础-支付宝-IOT 服务】";
    private static final String METHOD_QUERY = "（alipay.merchant.iot.device.query(IoT设备绑定关系查询) https://opendocs.alipay.com/pre-apis/01eeuq）";
    private static final String METHOD_BIND = "（alipay.merchant.iot.device.bind(IoT设备绑定门店) https://opendocs.alipay.com/pre-apis/01eeuo）";

    @Autowired
    AlipayApiProperties alipayApiProp;
    @Autowired
    BizLogSer bizLogSer;

    @SneakyThrows
    @Override
    public BaseAlipayIotQueryOut query(BaseAlipayIotQueryIn in) {
        log.info(StringUtil.join(BUSINESS_NAME, LogConstant.SERVICE, METHOD_QUERY, LogConstant.IN, JSONUtil.toStr(in)));
        /** 操作：调用支付宝接口 */
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConstant.COMMON_SERVER_URL, alipayApiProp.getAppid(), alipayApiProp.getPrivateKey(), AlipayConstant.COMMON_FORMAT, AlipayConstant.COMMON_CHARSET, alipayApiProp.getPublicKey(), AlipayConstant.COMMON_SIGN_TYPE);
        AlipayMerchantIotDeviceQueryRequest request = new AlipayMerchantIotDeviceQueryRequest();
        request.setBizContent(JSONUtil.toStr(in));
        AlipayMerchantIotDeviceQueryResponse response = alipayClient.execute(request);
        /** 操作：结果转换 */
        BaseAlipayIotQueryOut result = ObjectUtil.copy(response, BaseAlipayIotQueryOut.class);
        BaseAlipayCommonOut commonOut = result.commonOut(response);
        ObjectUtil.copy(commonOut, result);
        log.info(StringUtil.join(BUSINESS_NAME, LogConstant.SERVICE, METHOD_QUERY, LogConstant.RESULT, LogConstant.SUCCESS, JSONUtil.toStr(result)));
        return result;
    }

    @SneakyThrows
    @Override
    public BaseAlipayIotBindOut bind(BaseAlipayIotBindIn in) {
        log.info(StringUtil.join(BUSINESS_NAME, LogConstant.SERVICE, METHOD_BIND, LogConstant.IN, JSONUtil.toStr(in)));
        /** 操作：调用支付宝接口 */
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConstant.COMMON_SERVER_URL, alipayApiProp.getAppid(), alipayApiProp.getPrivateKey(), AlipayConstant.COMMON_FORMAT, AlipayConstant.COMMON_CHARSET, alipayApiProp.getPublicKey(), AlipayConstant.COMMON_SIGN_TYPE);
        AlipayMerchantIotDeviceBindRequest request = new AlipayMerchantIotDeviceBindRequest();
        request.setBizContent(JSONUtil.toStr(in));
        AlipayMerchantIotDeviceBindResponse response = alipayClient.execute(request);
        /** 操作：记录业务日志 */
        bizLogSer.save(new BizLogSave(new BizLogDataTemp(in, response), AlipayConstant.IOT_BIND_LOG));
        /** 操作：结果转换 */
        BaseAlipayIotBindOut result = ObjectUtil.copy(response, BaseAlipayIotBindOut.class);
        BaseAlipayCommonOut commonOut = result.commonOut(response);
        ObjectUtil.copy(commonOut, result);
        log.info(StringUtil.join(BUSINESS_NAME, LogConstant.SERVICE, METHOD_BIND, LogConstant.RESULT, LogConstant.SUCCESS, JSONUtil.toStr(result)));
        return result;
    }
}